#!/bin/bash
# ================================================
# 缤纷云对象存储一键挂载脚本
# 功能：自动安装bitifs客户端，配置凭证并挂载对象存储
# 特性：
# 1. 交互式输入AK/SK、桶名和挂载路径
# 2. 自动创建不存在的挂载目录
# 3. 自动配置systemd服务实现开机挂载
# 4. 详细的执行日志输出
# 版本: 1.1  作者 https://blog.hx99.net https://gitee.com/ev936/bitiful-oss
# ================================================

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 彩色输出函数
info() {
    echo -e "${GREEN}[信息]${NC} $1"
}

warn() {
    echo -e "${YELLOW}[警告]${NC} $1"
}

error() {
    echo -e "${RED}[错误]${NC} $1"
}

title() {
    echo -e "${BLUE}$1${NC}"
}

# 欢迎信息
title "================================================"
title " 缤纷云对象存储一键挂载脚本"
title "================================================"
info " 本脚本将帮助您完成以下操作："
info " 1. 下载并安装bitifs客户端"
info " 2. 配置访问凭证(AK/SK)"
info " 3. 挂载指定存储桶到本地目录"
info " 4. 配置开机自动挂载"
warn " 注意：此操作需要root权限执行"
title "================================================"
echo ""

# 检查root权限
if [ "$(id -u)" != "0" ]; then
    error "此脚本需要root权限执行!"
    exit 1
fi

# 日志函数
log() {
    info "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}

# 交互式输入配置信息
read -p "$(info '请输入Access Key ID: ')" AWS_ACCESS_KEY_ID
read -p "$(info '请输入Secret Access Key: ')" AWS_SECRET_ACCESS_KEY
read -p "$(info '请输入要挂载的存储桶名称: ')" BUCKET_NAME
read -p "$(info '请输入本地挂载路径(默认:/data/binfen-mount): ')" MOUNT_PATH

# 设置默认挂载路径
[ -z "$MOUNT_PATH" ] && MOUNT_PATH="/data/binfen-mount"

# 验证挂载路径格式
if [[ "$MOUNT_PATH" != /* ]]; then
    error "挂载路径必须是绝对路径(以/开头)"
    exit 1
fi

# 检查挂载路径是否存在
if [ ! -d "$MOUNT_PATH" ]; then
    warn "挂载路径 $MOUNT_PATH 不存在，正在创建..."
    mkdir -p "$MOUNT_PATH" || {
        error "创建目录失败! 请检查路径权限"
        exit 1
    }
    log "目录创建成功: $MOUNT_PATH"
fi

# 安装bitifs
log "开始安装bitifs客户端..."
yum install wget -y && wget -O /usr/local/bin/bitifs https://fanfan.s3.bitiful.net/bitifs_linux_amd64 || {
    error "bitifs下载失败!"
    exit 1
}
chmod +x /usr/local/bin/bitifs
log "bitifs安装完成"

# 配置凭证
log "正在配置访问凭证..."
mkdir -p /etc/bitifs && chmod 700 /etc/bitifs
cat > /etc/bitifs/credentials.env <<EOF
export AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID"
export AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY"
EOF
chmod 600 /etc/bitifs/credentials.env
log "凭证配置完成"

# 测试挂载
log "正在测试挂载..."
source /etc/bitifs/credentials.env
/usr/local/bin/bitifs "$BUCKET_NAME" "$MOUNT_PATH" && {
    log "测试挂载成功"
    umount "$MOUNT_PATH"
} || {
    error "测试挂载失败!"
    warn "可能原因:"
    info "1. AK/SK不正确"
    info "2. 存储桶不存在"
    info "3. 网络连接问题"
    exit 1
}

# 配置开机挂载
log "正在配置开机自动挂载..."
cat > /etc/systemd/system/bitifs-mount.service <<EOF
[Unit]
Description=Mount BitIFS Object Storage ($BUCKET_NAME to $MOUNT_PATH)
After=network.target

[Service]
Type=oneshot
ExecStart=/bin/bash -c 'source /etc/bitifs/credentials.env && /usr/local/bin/bitifs $BUCKET_NAME $MOUNT_PATH'
ExecStop=/bin/umount $MOUNT_PATH
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF

chmod 644 /etc/systemd/system/bitifs-mount.service
systemctl daemon-reload
systemctl enable bitifs-mount.service
systemctl start bitifs-mount.service

log "开机挂载配置完成"

# 最终状态检查
log "挂载状态:"
mount | grep "$MOUNT_PATH" || {
    error "挂载点未找到，可能有配置错误"
    exit 1
}

title "================================================"
log "挂载配置完成!"
info "存储桶: $BUCKET_NAME"
info "挂载点: $MOUNT_PATH"
warn "请检查以上信息是否正确"
title "================================================"